﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;

public partial class learning_test_ok : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private IAnswerService m_AnswerService { get; set; }
    private SessionHelper m_SessionHelper;
    private int m_AnswerId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_AnswerService = m_PostFactory.GetAnswerService();
        m_SessionHelper = new SessionHelper();
        m_AnswerId = QueryStringHelper.GetInteger("answer", 0);

        if (m_SessionHelper.Member == null)
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("請先登入會員。", "login.aspx"), false);
            return;
        }

        if (!IsPostBack)
        {            
            AnswerVO answerVO = m_AnswerService.GetAnswerById(m_AnswerId);            
            if (answerVO != null && answerVO.Member.MemberId == m_SessionHelper.Member.MemberId)
            {
                ltlPostTitle.Text = answerVO.PostTitle;
                hdnId.Value = answerVO.AnswerId.ToString();
                if (answerVO.Result == 1)
                {
                    lblPass.Visible = true;
                    lblNoPass.Visible = false;
                }
                else
                {
                    lblPass.Visible = false;
                    lblNoPass.Visible = true;
                }                
            }
            else
            {
                Response.Redirect("learning.aspx");
            }
        }
    }

    protected void btnSend_Click(object sender, ImageClickEventArgs e)
    {
        AnswerVO answerVO = m_AnswerService.GetAnswerById(int.Parse(hdnId.Value));
        if (answerVO != null)
        {
            answerVO.ApplyForVerification = true;
            answerVO.ApplyForVerificationStatus = 2;
            m_AnswerService.UpdateAnswer(answerVO);

            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJSAndRedirect("已送出時數證明申請。", "learning.aspx"), false);
        }
        else
        {
            Response.Redirect("learning.aspx");
        }
    }
}